/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.ui;

import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;

import lombok.Getter;

import org.apache.log4j.Level;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.wb.swt.SWTResourceManager;

import com.google.code.cubeirc.base.BaseTab;
import com.google.code.cubeirc.common.ApplicationInfo;
import com.google.code.cubeirc.config.Configuration;
import com.google.code.cubeirc.config.UserSettings;
import com.google.code.cubeirc.debug.DebuggerQueue;
import com.google.code.cubeirc.dialogs.AlertBox;
import com.google.code.cubeirc.sdk.CubePluginsHost;
import com.google.code.cubeirc.sdk.ICubePlugin;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ConfigForm extends BaseTab {
	@Getter
	private CTabFolder tfConf;
	private Button btnSave;
	private Button btnClose;
	private CTabItem tbtmCubeirc;
	private Composite composite_1;
	private Label lblNickname;
	private Text edtNickname;
	private Label lblAlternativeNickname;
	private Text edtAltNick;
	private Label lblEmail;
	private Text edtEmail;
	private Label lblRealName;
	private Text edtRealname;
	private Button btnDefaultEditorPrefernces;
	private Button btnMessagesColors;



	public ConfigForm(Composite composite, int style, String name) {
		super(composite, style, name);

		jbInit();
	}
	private void jbInit() {
		setLayout(new FormLayout());

		this.tfConf = new CTabFolder(this, SWT.BORDER);
		this.tfConf.setSimple(false);
		this.tfConf.setBorderVisible(false);
		FormData fd_tfConf = new FormData();
		fd_tfConf.left = new FormAttachment(0);
		fd_tfConf.right = new FormAttachment(100);
		fd_tfConf.top = new FormAttachment(0);
		this.tfConf.setLayoutData(fd_tfConf);
		this.tfConf.setSelectionBackground(Display.getCurrent().getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND_GRADIENT));

		this.btnSave = new Button(this, SWT.NONE);
		this.btnSave.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				saveAppConfiguration();
			}
		});
		fd_tfConf.bottom = new FormAttachment(this.btnSave, -6);

		this.tbtmCubeirc = new CTabItem(this.tfConf, SWT.NONE);
		this.tbtmCubeirc.setImage(SWTResourceManager.getImage(ConfigForm.class, "/com/google/code/cubeirc/resources/ApplicationIcon_small.png"));
		this.tbtmCubeirc.setText(ApplicationInfo.APP_NAME);

		this.composite_1 = new Composite(this.tfConf, SWT.NONE);
		this.tbtmCubeirc.setControl(this.composite_1);
		this.composite_1.setLayout(new FormLayout());
		
		this.lblNickname = new Label(this.composite_1, SWT.NONE);
		FormData fd_lblNickname = new FormData();
		fd_lblNickname.top = new FormAttachment(0, 10);
		fd_lblNickname.left = new FormAttachment(0, 10);
		this.lblNickname.setLayoutData(fd_lblNickname);
		this.lblNickname.setText("Nickname:");
		
		this.edtNickname = new Text(this.composite_1, SWT.BORDER);
		FormData fd_edtNickname = new FormData();
		fd_edtNickname.right = new FormAttachment(100, -279);
		fd_edtNickname.left = new FormAttachment(0, 10);
		this.edtNickname.setLayoutData(fd_edtNickname);
		
		this.lblAlternativeNickname = new Label(this.composite_1, SWT.NONE);
		fd_edtNickname.bottom = new FormAttachment(this.lblAlternativeNickname, -6);
		FormData fd_lblAlternativeNickname = new FormData();
		fd_lblAlternativeNickname.top = new FormAttachment(0, 59);
		fd_lblAlternativeNickname.left = new FormAttachment(this.lblNickname, 0, SWT.LEFT);
		this.lblAlternativeNickname.setLayoutData(fd_lblAlternativeNickname);
		this.lblAlternativeNickname.setText("Alternative nickname:");
		
		this.edtAltNick = new Text(this.composite_1, SWT.BORDER);
		FormData fd_edtAltNick = new FormData();
		fd_edtAltNick.right = new FormAttachment(this.edtNickname, 0, SWT.RIGHT);
		fd_edtAltNick.left = new FormAttachment(0, 10);
		fd_edtAltNick.top = new FormAttachment(this.lblAlternativeNickname, 6);
		this.edtAltNick.setLayoutData(fd_edtAltNick);
		
		this.lblEmail = new Label(this.composite_1, SWT.NONE);
		FormData fd_lblEmail = new FormData();
		fd_lblEmail.top = new FormAttachment(this.edtAltNick, 6);
		fd_lblEmail.left = new FormAttachment(this.lblNickname, 0, SWT.LEFT);
		this.lblEmail.setLayoutData(fd_lblEmail);
		this.lblEmail.setText("Email:");
		
		this.edtEmail = new Text(this.composite_1, SWT.BORDER);
		FormData fd_edtEmail = new FormData();
		fd_edtEmail.right = new FormAttachment(100, -268);
		fd_edtEmail.left = new FormAttachment(0, 10);
		fd_edtEmail.top = new FormAttachment(this.lblEmail, 6);
		this.edtEmail.setLayoutData(fd_edtEmail);
		
		this.lblRealName = new Label(this.composite_1, SWT.NONE);
		FormData fd_lblRealName = new FormData();
		fd_lblRealName.top = new FormAttachment(this.edtEmail, 6);
		fd_lblRealName.right = new FormAttachment(this.lblNickname, 0, SWT.RIGHT);
		this.lblRealName.setLayoutData(fd_lblRealName);
		this.lblRealName.setText("Real name:");
		
		this.edtRealname = new Text(this.composite_1, SWT.BORDER);
		FormData fd_edtRealname = new FormData();
		fd_edtRealname.right = new FormAttachment(100, -268);
		fd_edtRealname.left = new FormAttachment(0, 10);
		fd_edtRealname.top = new FormAttachment(this.lblRealName, 6);
		this.edtRealname.setLayoutData(fd_edtRealname);
		
		this.btnDefaultEditorPrefernces = new Button(this.composite_1, SWT.NONE);
		this.btnDefaultEditorPrefernces.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				EditorPreferences ed = new EditorPreferences(getShell(), SWT.NORMAL,ApplicationInfo.EDT_DEFAULT_NAME);
				ed.open();
			}
		});
		this.btnDefaultEditorPrefernces.setImage(SWTResourceManager.getImage(ConfigForm.class, "/com/google/code/cubeirc/resources/img_colors.png"));
		FormData fd_btnDefaultEditorPrefernces = new FormData();
		fd_btnDefaultEditorPrefernces.top = new FormAttachment(this.edtRealname, 9);
		fd_btnDefaultEditorPrefernces.left = new FormAttachment(this.lblNickname, 0, SWT.LEFT);
		this.btnDefaultEditorPrefernces.setLayoutData(fd_btnDefaultEditorPrefernces);
		this.btnDefaultEditorPrefernces.setText("Default editor prefernces");
		
		this.btnMessagesColors = new Button(this.composite_1, SWT.NONE);
		this.btnMessagesColors.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				new MessagesColorsForm(getShell(), SWT.NORMAL).open();
				
			}
		});
		this.btnMessagesColors.setImage(SWTResourceManager.getImage(ConfigForm.class, "/com/google/code/cubeirc/resources/img_lines.png"));
		FormData fd_btnMessagesColors = new FormData();
		fd_btnMessagesColors.top = new FormAttachment(this.btnDefaultEditorPrefernces, 0, SWT.TOP);
		fd_btnMessagesColors.right = new FormAttachment(this.edtEmail, 0, SWT.RIGHT);
		this.btnMessagesColors.setLayoutData(fd_btnMessagesColors);
		this.btnMessagesColors.setText("Messages colors");
		this.btnSave.setImage(SWTResourceManager.getImage(ConfigForm.class, "/com/google/code/cubeirc/resources/img_save.png"));
		FormData fd_btnSave = new FormData();
		fd_btnSave.bottom = new FormAttachment(100, -10);
		fd_btnSave.left = new FormAttachment(0, 10);
		fd_btnSave.right = new FormAttachment(0, 75);
		this.btnSave.setLayoutData(fd_btnSave);
		this.btnSave.setText("Save");

		this.btnClose = new Button(this, SWT.NONE);
		this.btnClose.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				Close();
			}
		});
		this.btnClose.setImage(SWTResourceManager.getImage(ConfigForm.class, "/com/google/code/cubeirc/resources/img_exit.png"));
		FormData fd_btnClose = new FormData();
		fd_btnClose.top = new FormAttachment(this.btnSave, 0, SWT.TOP);
		fd_btnClose.right = new FormAttachment(100, -10);
		this.btnClose.setLayoutData(fd_btnClose);
		this.btnClose.setText("Close");

		checkPlugins();
		tfConf.setSelection(tbtmCubeirc);
		loadAppConfiguration();
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	private void loadAppConfiguration()
	{
		edtNickname.setText(Configuration.currentUSConfig.getNickname());
		edtAltNick.setText(Configuration.currentUSConfig.getAltnickname());
		edtEmail.setText(Configuration.currentUSConfig.getEmail());
		edtRealname.setText(Configuration.currentUSConfig.getRealname());
		
	}
	
	private void saveAppConfiguration()
	{
		UserSettings us = new UserSettings(edtNickname.getText(), edtAltNick.getText(), edtRealname.getText(), edtEmail.getText(), Configuration.getCurrentUSConfig().getEditorsettings());
		
		Configuration.saveUStoFile(ApplicationInfo.CFG_SETTINGS, us);
		new AlertBox(composite_1.getShell(),ApplicationInfo.APP_FULL, "Settings saved!", SWT.ICON_INFORMATION | SWT.OK);
		Configuration.loadUSfromFile(ApplicationInfo.CFG_SETTINGS);
		Close();
		
	}
	private void checkPlugins()
	{
		addDebug(Level.INFO, "Checking for plugins configuration");
		ConcurrentLinkedQueue<ICubePlugin> plugs =  CubePluginsHost.getPlugins();

		if (!plugs.isEmpty())
		{
			for (Iterator<ICubePlugin> it = plugs.iterator(); it.hasNext();)
			{
				addPluginTab(it.next());			
			}
		}
	}

	private void addPluginTab(final ICubePlugin plugin)
	{

		asyncExec(new Runnable() {

			@Override
			public void run() {
				DebuggerQueue.addDebug(this, Level.INFO, "Adding plugin's configuration tab %s", plugin.getName());
				CTabItem item = new CTabItem(getTfConf(), SWT.NORMAL);		
				item.setText(plugin.getName());
				item.setImage(plugin.getConfigurationImage());
				item.setControl(plugin.getConfigurationPage());						
			}
		});
	}
}
